/*
 * jlb
 */
package com.jlb.star.service;

import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.StarRecordException;
import com.jlb.star.dao.StarRecordDAO;
import com.jlb.star.entity.StarRecordState;
import com.jlb.star.entity.StarRecord;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("starRecordSV")
@Slf4j
public class StarRecordSVImpl extends BaseSVImpl<StarRecord, Long> implements StarRecordSV {

    @Autowired
    private StarRecordDAO starRecordDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return starRecordDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(StarRecord entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象StarRecord
     *
     * @param id * @param userCode 用户编码
     * @return StarRecord
     */
    @Override
    public StarRecord load(java.lang.Long id, java.lang.String userCode) {
        if (id == null && userCode == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        return starRecordDAO.load(param);
    }

    /**
     * 加载一个对象StarRecord 通过id
     *
     * @param id
     * @return StarRecord
     */
    @Override
    public StarRecord loadById(java.lang.Long id) {
        if (id == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return starRecordDAO.loadById(id);
    }

    /**
     * 加载一个对象StarRecord 通过userCode
     *
     * @param userCode 用户编码
     * @return StarRecord
     */
    @Override
    public StarRecord loadByUserCode(java.lang.String userCode) {
        if (userCode == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return starRecordDAO.loadByUserCode(userCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 StarRecord 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, StarRecordState newState, StarRecordState... oldStates) {
        if (id == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        starRecordDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键userCode,oldStates 共同更新 StarRecord 的状态到newState状态
     *
     * @param userCode  用户编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByUserCode(java.lang.String userCode, StarRecordState newState, StarRecordState... oldStates) {
        if (userCode == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        starRecordDAO.updateStateByUserCode(userCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 StarRecord 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, StarRecordState state) {
        if (id == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        starRecordDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键userCode 更新 StarRecord 的状态到另一个状态
     *
     * @param userCode 用户编码
     * @param state    状态
     */
    @Override
    public void updateByUserCode(java.lang.String userCode, StarRecordState state) {
        if (userCode == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        starRecordDAO.updateByUserCode(userCode, state, new Date());
    }


    /**
     * 删除对象StarRecord
     *
     * @param id * @param userCode 用户编码
     * @return StarRecord
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String userCode) {
        if (id == null && userCode == null) {
            throw new StarRecordException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        starRecordDAO.delete(param);
    }


    /**
     * 查询StarRecord分页
     *
     * @param starRecord 对象
     * @param offset     查询开始行
     * @param limit      查询行数
     * @return List<StarRecord>
     */
    @Override
    public List<StarRecord> list(StarRecord starRecord, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (starRecord != null) {
            map = JSON.parseObject(JSON.toJSONString(starRecord, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return starRecordDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(StarRecord starRecord) {
        Map<String, Object> map = null;
        if (starRecord != null) {
            map = JSON.parseObject(JSON.toJSONString(starRecord, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return starRecordDAO.count(map);
    }

    /**
     * 查询StarRecord分页
     *
     * @param id     * @param userCode  用户编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<StarRecord>
     */
    @Override
    public List<StarRecord> list(java.lang.Long id, java.lang.String userCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        return starRecordDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String userCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        return starRecordDAO.count(param);
    }

}
